#ifndef CMESH_H
#define CMESH_H

// STD
#include <vector>
#include <string>

/*** ***/
#include <QtOpenGL>
#include "cpoint3f.h"
#include "cvector3f.h"

// PROJECT
#include "cvertex.h"
#include "cedge.h"
#include "cface.h"
#include "cdrawable.h"
#include "craytracable.h"

class CMesh : public CDrawable, public CRayTracable
{
public:
    CMesh();
    ~CMesh();

    bool					LoadOBJ(const char *);
    bool					LoadOBJ(std::string);
    bool					SaveOBJ(char *);
    bool					SaveOBJ(std::string);
    void					makeCopyOf(CMesh *);
    void					computeNormals();
    void					computeEdges();
    void					pairEdges();
    void					checkVerticesNormals();

    void					normalizeSize();

    bool					isBuilt();
    bool					doesEdgeExist(CVertex*, CVertex *);
    CEdge *					getEdgeFromVertices(CVertex*, CVertex *);
    int						findIndOfVertex(CVertex*);

    CVertex *				getVertexByPos(CPoint3f P);

    void					clearAllList();
    void					clearVertexList();
    void					clearEdgeList();
    void					clearFaceList();


    void					paint();
    void					setupBoundingSphere();
    void					setupBoundingBox();

    std::string             className();

    //Return true if at least one intersection point actually exists
    //and then set the point to the first of these points and
    //the vector to the normal at this point, return false otherwise
    bool                    intersect(CRay * Ray, CPoint3f& P, CVector3f& V);
    void                    setMaterial(CMaterial mat);

    std::vector<CVertex*>   m_vVertexList;
    std::vector<CEdge*>		m_vEdgeList;
    std::vector<CFace*>		m_vFaceList;

protected:

    bool					m_bVerticesNormals;
    bool					m_bBuilt;
};

#endif // CMESH_H
